{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# String.format()字符串常规类型格式化的两种重载方式\n",
    "- format(String format, Object… args) 新字符串使用本地语言环境，制定字符串格式和参数生成格式化的新字符串。\n",
    "- format(Locale locale, String format, Object… args) 使用指定的语言环境，制定字符串格式和参数生成格式化的字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 例子一\n",
    "\n",
    "|转换符|\t详细说明|\t示例|\n",
    "|:-----|:----------|:------|\n",
    "|%s|\t字符串类型|\t“喜欢请收藏”|\n",
    "|%c|\t字符类型|\t‘m’|\n",
    "|%b|\t布尔类型|\ttrue|\n",
    "|%d|\t整数类型|（十进制）|\t88|\n",
    "|%x|\t整数类型|（十六进制）|\tFF|\n",
    "|%o|\t整数类型|（八进制）|\t77|\n",
    "|%f|\t浮点类型|\t8.888|\n",
    "|%a|\t十六进制浮点类型|\tFF.35AE|\n",
    "|%e|\t指数类型|\t9.38e+5|\n",
    "|%g|\t通用浮点类型（f和e类型中较短的）|\t不举例(基本用不到)|\n",
    "|%h|\t散列码|\t不举例(基本用不到)|\n",
    "|%%\t|百分比类型|\t％(%特殊字符%%才能显示%)|\n",
    "|%n|\t换行符|\t不举例(基本用不到)|\n",
    "|%tx|\t日期与时间类型（x代表不同的日期与时间转换符)|\t不举例(基本用不到)|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi,小超\n",
      "Hi,小超 是个 大帅哥\n",
      "字母c的大写是：C \n",
      "布尔结果是：false \n",
      "100的一半是：50 \n",
      "100的16进制数是：64 \n",
      "100的8进制数是：144 \n",
      "50元的书打8.5折扣是：42.500000 元\n",
      "上面价格的16进制数是：0x1.54p5 \n",
      "上面价格的指数表示：4.250000e+01 \n",
      "上面价格的指数和浮点数结果的长度较短的是：42.5000 \n",
      "上面的折扣是85% \n",
      "字母A的散列码是：41 \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "java.io.PrintStream@3b5d27c2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "String str=null;  \n",
    "str=String.format(\"Hi,%s\", \"小超\");  \n",
    "System.out.println(str);\n",
    "\n",
    "str=String.format(\"Hi,%s %s %s\", \"小超\",\"是个\",\"大帅哥\");            \n",
    "System.out.println(str);\n",
    "\n",
    "System.out.printf(\"字母c的大写是：%c %n\", 'C');  \n",
    "System.out.printf(\"布尔结果是：%b %n\", \"小超\".equals(\"帅哥\"));  \n",
    "System.out.printf(\"100的一半是：%d %n\", 100/2);  \n",
    "System.out.printf(\"100的16进制数是：%x %n\", 100);  \n",
    "System.out.printf(\"100的8进制数是：%o %n\", 100);  \n",
    "System.out.printf(\"50元的书打8.5折扣是：%f 元%n\", 50*0.85);  \n",
    "System.out.printf(\"上面价格的16进制数是：%a %n\", 50*0.85);  \n",
    "System.out.printf(\"上面价格的指数表示：%e %n\", 50*0.85);  \n",
    "System.out.printf(\"上面价格的指数和浮点数结果的长度较短的是：%g %n\", 50*0.85);  \n",
    "System.out.printf(\"上面的折扣是%d%% %n\", 85);  \n",
    "System.out.printf(\"字母A的散列码是：%h %n\", 'A'); "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 例子二"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|标志|\t说明|\t示例|\t结果|\n",
    "|:---|:-------|:----|:----|\n",
    "|+|\t为正数或者负数添加符号|\t(“%+d”,15)|\t+15|\n",
    "|0|\t数字前面补0(加密常用)|\t(“%04d”, 99)|\t0099|\n",
    "|空格|\t在整数之前添加指定数量的空格|\t(“% 4d”, 99)|\t99|\n",
    "|,|\t以“,”对数字分组(常用显示金额)|\t(“%,f”, 9999.99)|\t9,999.990000|\n",
    "|(|\t使用括号包含负数|\t(“%(f”, -99.99)|\t(99.990000)|\n",
    "|#|\t如果是浮点数则包含小数点，如果是16进制或8进制则添加0x或0|\t(“%#x”, 99)(“%#o”, 99)|\t0x63 0143|\n",
    "|<|\t格式化前一个转换符所描述的参数|(\"%f和%<3.2f\", 99.45)(\"%d,%2$s\", 99,\"abc\")|(99.450000和99.45) (99,abc)|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99.450000和99.45\n",
      "99,abc\n"
     ]
    }
   ],
   "source": [
    "System.out.println(String.format(\"%f和%<3.2f\",99.45));\n",
    "System.out.println(String.format(\"%d,%2$s\", 99,\"abc\"));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第一个例子中有说到 %tx x代表日期转换符\n",
    "\n",
    "|标志|\t说明|\t示例|\n",
    "|:---|:-------|:----|\n",
    "|c|\t包括全部日期和时间信息|\t星期六 十月 27 14:21:20 CST 2007|\n",
    "|F|\t“年-月-日”格式|\t2007-10-27|\n",
    "|D|\t“月/日/年”格式|\t10/27/07|\n",
    "|r|\t“HH:MM:SS PM”格式（12时制）|\t02:25:51 下午|\n",
    "|T|\t“HH:MM:SS”格式（24时制）|\t14:28:16|\n",
    "|R|\t“HH:MM”格式（24时制）|\t14:28|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "全部日期和时间信息：Mon Aug 17 15:39:42 UTC 2020\n",
      "年-月-日格式：2020-08-17\n",
      "月/日/年格式：08/17/20\n",
      "HH:MM:SS PM格式（12时制）：03:39:42 PM\n",
      "HH:MM:SS格式（24时制）：15:39:42\n",
      "HH:MM格式（24时制）：15:39"
     ]
    },
    {
     "data": {
      "text/plain": [
       "java.io.PrintStream@3b5d27c2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Date date = new Date(); \n",
    "System.out.println(date.getHours());\n",
    "\n",
    "//c的使用  \n",
    "System.out.printf(\"全部日期和时间信息：%tc%n\",date);          \n",
    "//f的使用  \n",
    "System.out.printf(\"年-月-日格式：%tF%n\",date);  \n",
    "//d的使用  \n",
    "System.out.printf(\"月/日/年格式：%tD%n\",date);  \n",
    "//r的使用  \n",
    "System.out.printf(\"HH:MM:SS PM格式（12时制）：%tr%n\",date);  \n",
    "//t的使用  \n",
    "System.out.printf(\"HH:MM:SS格式（24时制）：%tT%n\",date);  \n",
    "//R的使用  \n",
    "System.out.printf(\"HH:MM格式（24时制）：%tR\",date);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对整数进行格式化：%[index$][标识][最小宽度]转换方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "格式化字符串由4部分组成，特殊的格式常以%index$开头，index从1开始取值，表示将第index个参数拿进来进行格式化，[最小宽度]的含义也很好理解，就是最终该整数转化的字符串最少包含多少位数字。剩下2个部分的含义：\n",
    "\n",
    "**标识：**\n",
    "\n",
    "|转换符|含义|\n",
    "|:---|:---|\n",
    "|-|在最小宽度内左对齐，不可以与\"用0填充\"同时使用|\n",
    "|#|只适用于8进制和16进制，8进制时在结果前面增加一个0，16进制时在结果前面增加0x|\n",
    "|+|结果总是包括一个符号(一般情况下只适用于10进制，若对象为BigInteger才可以用于8进制和16进制)|\n",
    "|空格|正值前加空格，负值前加负号(一般情况下只适用于10进制，若对象为BigInteger才可以用于8进制和16进制)|\n",
    "|0|结果将用零来填充|\n",
    "|,|只适用于10进制，每3位数字之间用\"，\"分隔|\n",
    "|(|若参数是负数，则结果中不添加负号而是用圆括号把数字括起来(同'+'具有同样的限制)|\n",
    "\n",
    "**转换方式：**\n",
    "\n",
    "|转换符|含义|\n",
    "|:---|:---|\n",
    "|d|十进制|\n",
    "|o|八进制|\n",
    "|x/X|十六进制|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0003,123\n",
      "      -31\n",
      "-31      \n",
      "     (31)\n",
      "   0x1639\n",
      "123,456,789\n"
     ]
    }
   ],
   "source": [
    "System.out.println(String.format(\"%1$,09d\", -3123));\n",
    "System.out.println(String.format(\"%1$9d\", -31));\n",
    "System.out.println(String.format(\"%1$-9d\", -31));\n",
    "System.out.println(String.format(\"%1$(9d\", -31));\n",
    "System.out.println(String.format(\"%1$#9x\", 5689));\n",
    "System.out.println(String.format(\"%1$,9d\", 123456789));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对浮点数进行格式化：%[index$][标识][最少宽度][.精度]转换方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以看到，浮点数的转换多了一个\"精度\"选项，可以控制小数点后面的位数。\n",
    "\n",
    "**标识：**\n",
    "\n",
    "|转换符|含义|\n",
    "|:---|:---|\n",
    "|-|在最小宽度内左对齐，不可以与\"用0填充\"同时使用|\n",
    "|+|结果总是包括一个符号|\n",
    "|空格|正值前加空格，负值前加负号|\n",
    "|0|结果将用零来填充|\n",
    "|,|每3位数字之间用\"，\"分隔(只适用于fgG的转换)|\n",
    "|(|若参数是负数，则结果中不添加负号而是用圆括号把数字括起来(只适用于eEfgG的转换)|\n",
    "\n",
    "**转换方式：**\n",
    "\n",
    "|转换符|含义|\n",
    "|:---|:---|\n",
    "|e/E|结果被格式化为用计算机科学记数法表示的十进制数|\n",
    "|f|结果被格式化为十进制普通表示方式|\n",
    "|g/G|根据具体情况，自动选择用普通表示方式还是科学计数法方式|\n",
    "|a/A|结果被格式化为带有效位数和指数的十六进制浮点数|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对字符进行格式化：\n",
    "\n",
    "对字符进行格式化是非常简单的，c表示字符，标识中'-'表示左对齐，其他就没什么了。\n",
    "\n",
    "|转换符|含义|\n",
    "|:-----|:---|\n",
    "|c|\t字符类型|\n",
    "|s|\t字符串类型|"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.8+10-post-Ubuntu-0ubuntu120.04"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "150.55px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
